﻿using ACC.Extensions.PCClient.Entities;
using Spire.Pdf.General.Render.Decode.Jpeg2000.j2k.util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;


namespace ACC.Extensions.PCClient.UserControls
{
    /// <summary>
    /// BarCodeListControl.xaml 的交互逻辑
    /// </summary>
    public partial class BarCodeListControl : UserControl
    {
        //前端列表数据源
        public  List<BarCodeItem> Barcodes;

        public BarCodeListControl()
        {
            InitializeComponent();
            Init();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
         //    Dispatcher.Invoke(new Action(() =>
        //    {
                dgBarcode.ItemsSource = null;
            Barcodes = new List<BarCodeItem>();
            dgBarcode.ItemsSource = Barcodes;
            //      }));
        }

        public void FillBarCodes(List<BarCodeItem> barcodes)
        {
         //   Dispatcher.Invoke(new Action(() =>
        //    {
                dgBarcode.ItemsSource = null;
                Barcodes = barcodes;
                dgBarcode.ItemsSource = Barcodes;
          //  }));     
        }

        /// <summary>
        /// 是否已经扫完全部条码
        /// </summary>
        public bool IsFull
        {
            get
            {
                if (Barcodes != null && Barcodes.Count > 0)
                {
                    foreach (var item in Barcodes)
                    {
                        if (item.IsCurrentOP)//零件属于当前工位
                        {
                            //需要扫的条码为空，返回false
                            if (string.IsNullOrEmpty(item.IDValue) || item.Status != BarcodeStatus.OK)
                            {
                                return false;
                            }
                        }
                        else//零件不属于当前工位
                        {
                            //为主条码，且为空，返回false
                            if (item.LeadNo && string.IsNullOrEmpty(item.IDValue))
                            {
                                return false;
                            }
                        }
                    }
                    
                    ////条码列表里的所有零件都不在当前OP绑定，返回true
                    //if (!Barcodes.ToList().Exists(b => b.IsCurrentOP))
                    //{
                    //    return true;
                    //}

                    ////条码列表有需要当前工位绑定的条码，且当前为空或者条码错误，返回false
                    //return !Barcodes.ToList().Exists(b => (b.IsCurrentOP) &&
                    //                                       (b.Status==BarcodeStatus.OK || string.IsNullOrEmpty(b.IDValue))
                    //                                 );
                }
                return true;
            }
        }              

        /// <summary>
        /// 主条码
        /// </summary>
        public BarCodeItem MainItem
        {
            get
            {
                if (Barcodes != null && Barcodes.Count > 0)
                {
                    var v = Barcodes.FirstOrDefault(p => p.LeadNo);
                    if (v != null)
                        return v;
                        //return Barcodes[0];
                    return v;
                }
                return null;
            }
        }

        /// <summary>
        /// 主条码有值
        /// </summary>
        public bool HasUnitSN
        {
            get 
            {
                if (MainItem != null)
                {
                    return !string.IsNullOrEmpty(MainItem.IDValue);
                }
                return false;
            }
        }

        /// <summary>
        /// 读取条码
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool OnReadBarcode(string barcode, out string msg)
        {
            msg = "";
            if (IsFull)
            {
                msg = "条码列表已满,无需扫码";
                return false;
            }

            if (!string.IsNullOrEmpty(barcode) && null != Barcodes.FirstOrDefault(c => c.IDValue == barcode))
                return true;

            foreach (BarCodeItem item in Barcodes)
            {
                if (string.IsNullOrEmpty(item.IDValue) || item.Status != BarcodeStatus.OK)
                {
                    if (item.IsCurrentOP)
                    {
                        //匹配正则
                        if (!String.IsNullOrEmpty(item.IDPattern) && !Regex.IsMatch(barcode, item.IDPattern))
                        {
                            continue;
                        }

                        //找到匹配的IDName
                        item.IDValue = barcode;
                        item.Status = BarcodeStatus.OK;

                        return true;
                    }
                }
            }

            msg = "未找到匹配的条码";
            return false;
        }     

        /// <summary>
        /// 删除条码的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            BarCodeItem selectBarCode = dgBarcode.SelectedItem as BarCodeItem;
            if (selectBarCode != null)
            {
                selectBarCode.IDValue = "";
                selectBarCode.Status = BarcodeStatus.Init;
            }
           //((BarCodeItem)dgBarcode.SelectedItem).IDValue="";
        }

        /// <summary>
        /// 清除所有条码内容
        /// </summary>
        public void ClearAllBarcodes()
        {
            if (Barcodes != null && Barcodes.Count > 0)
            {
                foreach (BarCodeItem bi in Barcodes)
                {
                    bi.IDValue = "";
                }
            }
        }

        /// <summary>
        /// 依据IDName填充条码
        /// </summary>
        /// <param name="bi"></param>
        public void FillBarcodeValueByName(BarCodeItem bi)
        {
            if (bi != null)
            {
                var v = Barcodes.FirstOrDefault(p => p.IDName == bi.IDName && string.IsNullOrEmpty(p.IDValue));
                if (v != null)
                {
                    v.IDValue = bi.IDValue;
                    v.Status = BarcodeStatus.OK;
                }
            }
        }

        public void FillBarcodeValueByName(string idname, string idvalue)
        {
            if (!string.IsNullOrEmpty(idname))
            {
                var v = Barcodes.FirstOrDefault(p => p.IDName == idname && string.IsNullOrEmpty(p.IDValue));
                if (v != null)
                {
                    v.IDValue = idvalue;
                    v.Status = BarcodeStatus.OK;
                }
            }
        }

        /// <summary>
        /// 匹配条码
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public bool MatchBarcode(string barcode)
        {
            if (Barcodes == null || Barcodes.Count < 1)
            {
                return false;
            }

            foreach (BarCodeItem bi in Barcodes)
            {
                if (String.IsNullOrEmpty(bi.IDPattern))
                {
                    //没有配置表达式时，无论什么格式的条码都能通过
                    return true;
                }
                return Regex.IsMatch(barcode, bi.IDPattern);
            }
            return true;            
        }             
    }    
}
